package com.zgjkhis.modules.outpatient.rest;

import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.outpatient.domain.Account;
import com.zgjkhis.modules.outpatient.domain.AccountLabel;
import com.zgjkhis.modules.outpatient.service.AccountLabelService;
import com.zgjkhis.modules.outpatient.service.AccountService;
import com.zgjkhis.modules.system.domain.Label;
import com.zgjkhis.modules.system.service.LabelService;
import com.zgjkhis.modules.util.CharUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @date 2018-11-23
 */
@Api(tags = "门诊：患者标签管理")
@RestController
@RequestMapping("/accountlable")
public class AccountLabelController {

    private static final String ENTITY_NAME = "accountlable";
    private final AccountLabelService accountLabelService;
    private final AccountService accountService;
    private final LabelService labelService;

    public AccountLabelController(AccountService accountService, AccountLabelService accountLabelService, LabelService labelService) {
        this.accountLabelService = accountLabelService;
        this.accountService = accountService;
        this.labelService = labelService;
    }


    @Log("患者标签管理")
    @ApiOperation("患者标签管理")
    @PostMapping(value = "/submit")
//    @PreAuthorize("@el.check('account:add')")
    public ResponseEntity<Object> create(@RequestBody AccountLabel resources) {
        if (CharUtil.isEmpty(resources.getId()) && (CharUtil.isEmpty(resources.getAccountId()) || CharUtil.isEmpty(resources.getLabelId()))) {
            throw new ServiceException("参数异常");
        }
        if (!CharUtil.isEmpty(resources.getId())) {
            //删除管理
            accountLabelService.removeById(resources.getId());
        } else {
            //新增管理
            Account account = accountService.getById(resources.getAccountId());
            if (CharUtil.isEmpty(account)) {
                throw new ServiceException("查无此人");
            }
            Label label = labelService.getById(resources.getLabelId());
            if (CharUtil.isEmpty(label)) {
                throw new ServiceException("标签不存在");
            }
            AccountLabel accountLabel = new AccountLabel();
            accountLabel.setAccountId(resources.getAccountId());
            accountLabel.setLabelId(resources.getLabelId());
            accountLabelService.save(accountLabel);
        }
        return new ResponseEntity<>(true, HttpStatus.OK);

    }


}
